﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using ExpertMIS.UserControl;
using IBatisNet.Common.Utilities;

namespace ExpertMIS.Columns
{
    public partial class ColumnView : System.Web.UI.Page
    {
        private ADMColumnBroker _columnBroker = new ADMColumnBroker();
        private ADMColumnEntity _columnEntity = null;
        private ADMArticleBroker _articleBroker = new ADMArticleBroker();
        private PagingSize _pagingSize = new PagingSize();

        private void BuildPagingSize()
        {
            _pagingSize.Limit = 10;

            int pagingStart = 1;
            if (!int.TryParse(Request["start"], out pagingStart) || pagingStart < 1)
            {
                pagingStart = 1;
            }

            _pagingSize.Start = pagingStart;
        }
        
        private void RefreshColumn(string columnOID)
        {
            int articleCount = 0;
            if (!int.TryParse(ExpertMIS.Codes.Utility.GetEnvironmentValue("column.articlecount"), out articleCount))
            {
                articleCount = 15; // 默认
            }
            _columnEntity = _columnBroker.GetColumnEntityByOID(columnOID);

            BuildPagingSize();

            repArticles.DataSource = _articleBroker.GetArticlesByColumn(columnOID, _pagingSize);
            repArticles.DataBind();
        }

        protected string GetColumnName()
        {
            if (null != _columnEntity)
            {
                return _columnEntity.Name;
            }

            return string.Empty;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            RefreshColumn(Request["oid"]);
            if(Master is Column)
                ((Column)Master).OnLoadLeftPanels += new EventHandler(ColumnView_OnLoadLeftPanels);
        }

        private void ColumnView_OnLoadLeftPanels(object sender, EventArgs e)
        {
            ucBulletinList bulletinList = LoadControl("~/UserControl/ucBulletinList.ascx") as ucBulletinList;
            if (bulletinList != null)
            {
                ((Column)Master).LeftPanel.Controls.Add(bulletinList);
                bulletinList.ShowTitle = true;
                bulletinList.LengthVariable = "ColumnView";
                bulletinList.ShowNewIcon = false;
                bulletinList.BulletinCatalog = BulletinCatalog.OrganizationBulletin;
            }
        }

        #region 分页信息

        protected string GetPriorPageIndex()
        {
            if (_pagingSize.Start - 1 > 0)
                return (_pagingSize.Start - 1).ToString();
            else
                return _pagingSize.Start.ToString();
        }

        protected string GetNextPageIndex()
        {
            if (_pagingSize.Start + 1 <= (int)System.Math.Ceiling((double)_pagingSize.Total / _pagingSize.Limit))
                return (_pagingSize.Start + 1).ToString();
            else
                return GetLastPageIndex();
        }

        protected string GetLastPageIndex()
        {
            return ((int)System.Math.Ceiling((double)_pagingSize.Total / _pagingSize.Limit)).ToString();
        }

        protected string GetPageIndex()
        {
            return ((int)System.Math.Ceiling((double)_pagingSize.Total / _pagingSize.Limit)).ToString();
        }

        protected string GetRecordCount()
        {
            if (_pagingSize.Total < 0) return "0";
            return _pagingSize.Total.ToString();
        }

        #endregion
    }
}
